<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/7
 * Time: 15:44
 */
namespace app\basketball\controller;
use app\basketball\model\AdminGroup;
use app\basketball\model\AdminRule;
use app\basketball\model\AdminRoleGroup;
use app\basketball\model\Adminuser;
use think\Controller;
use think\Cookie;

class BassController extends Controller{

//    /**
//     *  判断用户是否登录
//     */
    public function _initialize()
    {
        $username = Cookie::get('username');
        if(is_null($username))
        {
            $this->error('您还没有登录','admin/login/login');
        }
    }

    /**
     *  获取用户权限
     */
    public function getMenu(){
        $is_uid = $this->getUsername();
        $uid = $is_uid ? : 0;
        //获取当前用户的权限组
        $rules = Adminuser::where('id',$uid)->find();
        $rules['group_id'] = AdminGroup::where('uid',$uid)->value('group_id');
        $rules['rules'] = AdminRoleGroup::where('id',$rules['group_id'])->value('rules');
        if(empty($rules['group_id']) || empty($rules['rules'])){

            exit('你不具备任何管理权限无法登录');
        }
        //判断当前用户是否是超级管理员
//        if($rules['group_id'] == 1){
//            $arrWhere[] = ['p_id','=',0];
//            $arrWhere2[] = ['p_id','!=',0];
//            $arrWhere2[] = ['is_show','=',1];
//        }else{
//            $arrWhere[] = ['id','in', $rules['rules']];
//            $arrWhere[] = ['p_id','=',0];
//            $arrWhere2[] = ['p_id','!=',0];
//            $arrWhere2[] = ['id','in', $rules['rules']];
//            $arrWhere2[] = ['is_show','=',1];
//
//        }
        $parent = AdminRule::where('id','in',$rules['rules'])->where('p_id',0)->where('is_show',1)->select();
        $son = AdminRule::where('id','in',$rules['rules'])->where('p_id','<>',0)->where('is_show',1)->select();
        $menu = [];
        foreach ($parent as $k=>$v){

            foreach ($son as $k1=>$v1){

                if($v1['p_id']==$v['id']){

                    $menu[$k]['parentMenu'] = $v;
                    $menu[$k]['sonMenu'][]  = $v1;
                }
            }
        }
        return $menu;

    }

    /**
     * 获取登录用户名
     */
    public function getUsername(){

        $username = Cookie::get('username');
        $uid = Adminuser::where('username',$username)->value('id');
        if(empty($uid)){
            $this->error('您还没有登录','admin/login/login');
        }
        return $uid;
    }

 }